class Detail {
  constructor(detail) {
    this.orderNo = detail.orderNo | "";
    this.statusType = detail.statusType || "";
    this.status = detail.status || "";
    this.id = detail.id || "";
    this.createTime = detail.createTime || "";
  }

  get statusMsg() {
    let str;
    if (this.status == "ordered") {
      str = "支付成功，正在通知厨房";
    } else if (this.status == "order-fail") {
      str = "通知厨房失败，请申请退款";
    } else if (this.status == "refund-success") {
      str = "订单取消，已退款";
    } else if (this.status == "received") {
      str = "通知厨房成功，请凭号领餐";
    } else if (this.status == "query") {
      str = "正在查询订单";
    } else if (this.status == "cancel") {
      str = "订单取消";
    } else {
      str = "statusMsg:" + this.status;
    }
    return str;
  }
}

export default class ProcessStatus {
  constructor(processStatus) {
    processStatus = processStatus || {};
    this.id = processStatus.id || "";
    this.orderNo = processStatus.orderNo || "";
    this.statusType = processStatus.statusType || "";
    this.createTime = processStatus.createTime || 0;
    this.details = [];
    this.countdown = "";
    processStatus.detail && processStatus.detail.forEach(v => {
      //忽视noticed
      if (v.status != "noticed") {
        this.addDetail(v);
      }
    })
  }

  get status() {
    let l = this.details.length;
    return l ? this.details[l - 1].status : "order-init";
  }

  get processStatusMsg() {
    let str;
    if (this.status == "ordered") {
      str = "正在通知厨房  " + this.countdown;
    } else if (this.status == "order-fail") {
      str = "通知厨房失败，请申请退款"
    } else if (this.status == "received") {
      str = ""
    } else if (this.status == "refund-success") {
      str = "订单取消，已退款"
    } else if (this.status == "order-init") {
      str = "正在通知厨房  " + this.countdown;
    } else if (this.status == "query") {
      str = "正在查询订单";
    } else if (this.status == "cancel") {
      str = "订单取消";
    } else {
      str = "processStatusMsg:" + this.status;
    }
    return str;
  }

  get processStatusTitle() {
    if (this.status == "received") {
      // : "取餐号: " item.serialNo.substr(item.serialNo.indexOf('-') + 1)
      return "订单成功";
    } else {
      const str = this.processStatusMsg;
      if (str.indexOf("正在通知") > -1) {
        return "正在通知厨房";
      }
      let i = str.indexOf('，');
      return i > -1 ? str.substr(i + 1, str.length) : str;
    }
  }

  emptyProcessStatus() {
    this.details = [];
  }

  addDetailFail(detail) {
    const _detail = {
      "id": "1172650e6c3b45d2a4f27b2d90623e38",
      "orderNo": "order.orderId",
      "statusType": "wx-fast",
      "status": "order-fail",
      "createTime": (detail && detail.createTime) || new Date().getTime()
    }
    this.addDetail(_detail);
  }

  addDetailReceived(detail) {
    const _detail = {
      "id": "1172650e6c3b45d2a4f27b2d90623e38",
      "orderNo": "order.orderId",
      "statusType": "wx-fast",
      "status": "received",
      "createTime": (detail && detail.createTime) || new Date().getTime()
    }
    this.addDetail(_detail);
  }
  addDetailRefundSuccess(detail) {
    const _detail = {
      "id": "1172650e6c3b45d2a4f27b2d90623e38",
      "orderNo": "order.orderId",
      "statusType": "wx-fast",
      "status": "refund-success",
      "createTime": (detail && detail.createTime) || new Date().getTime()
    }
    this.addDetail(_detail);
  }
  addDetailOrdered(detail) {
    const _detail = {
      "id": "1172650e6c3b45d2a4f27b2d90623e38",
      "orderNo": "LL3WWISTDTMY9G9ON60CXYUXB",
      "statusType": "wx-fast",
      "status": "ordered",
      "createTime": (detail && detail.createTime) || new Date().getTime()
    }
    this.addDetail(_detail);
  }
  addDetailCancel(detail) {
    const _detail = {
      "id": "1172650e6c3b45d2a4f27b2d90623e38",
      "orderNo": "LL3WWISTDTMY9G9ON60CXYUXB",
      "statusType": "wx-fast",
      "status": "cancel",
      "createTime": (detail && detail.createTime) || new Date().getTime()
    }
    this.addDetail(_detail);
  }

  replaceDetail(detail) {
    this.details.pop();
    this.addDetail(detail)
  }

  addDetail(detail) {
    var _detaild = new Detail(detail);
    this.details.push(_detaild);
  }

  setCountDown(str) {
    this.countdown = str;
  }
}
